﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text;
using System.Data;

namespace PeisongWeb
{
    public partial class seller_companyInfo : customerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (logic.customer.isAdmin())
                {
                    drpBind();
                    getDetails();
                    menu1.menucss = "3";
                }
                else
                {
                    Response.Write("<script lanuage=javascript>location='javascript:history.go(-1)'</script>");
                }
            }
        }
        private void drpBind()
        {
            ddl_Area.DataSource = logic.sysCode.areaList();
            ddl_Area.DataBind();

            ddl_Employees.DataSource = logic.sysCode.companySizeList();
            ddl_Employees.DataBind();
        }
        protected void getDetails()
        {
            StringBuilder where = new StringBuilder();
            where.Append("companyId=" + logic.customer.companyId);
            DataTable dt = DAL.DbHelper.ExecuteDataTableDynamic("*", "[dbo].[vw_Company_list]", where.ToString(), "companyId");
            DataRow dr = dt.Rows[0];

            txt_companyName.Value = dr["company"].ToString();
            ddl_Area.SelectedIndex = ddl_Area.Items.IndexOf(ddl_Area.Items.FindByValue(dr["areaID"].ToString()));
            txt_Address.Text = dr["address"].ToString();
            txt_location.Value = dr["location"].ToString();
            txt_phone.Value = dr["phone"].ToString();
            txt_fax.Value = dr["fax"].ToString();
            txt_postCode.Value = dr["postcode"].ToString();
            ddl_Employees.SelectedIndex = ddl_Employees.Items.IndexOf(ddl_Employees.Items.FindByValue(dr["employees"].ToString()));
            txt_contact.Value = dr["contact"].ToString();
            txt_IDCard.Value = dr["idCard"].ToString();
            txt_Mobile.Value = dr["mobile"].ToString();
            txt_Email.Value = dr["email"].ToString();

            string type = logic.company.getById(logic.customer.companyId).Type;

            if (type == "1")
            {
                menu1.menucss = "6";
            }
            else if (type == "2")
            {
                li_location.Visible = false;
                menu1.menucss = "7";
            }
            else if (type == "3")
            {
                menu1.menucss = "3";
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (Request.Headers["Accept"] != "*/*")
            {
                CompanyEntity cp = new CompanyEntity();
                cp.CompanyId = logic.customer.companyId;
                cp.AreaID = Convert.ToInt32(ddl_Area.SelectedValue);
                cp.Address = txt_Address.Text;
                cp.Location = txt_location.Value;
                cp.Phone = txt_phone.Value;
                cp.Fax = txt_fax.Value;
                cp.Postcode = txt_postCode.Value;
                cp.Employees = ddl_Employees.SelectedValue;
                cp.Contact = txt_contact.Value;
                cp.IdCard = txt_IDCard.Value;
                cp.Mobile = txt_Mobile.Value;
                cp.Email = txt_Email.Value;
                logic.company.updatePartialSeller(cp);

                Library.Script.ClientMsg("修改成功！");
            }
        }
    }
}
